package com.bkhech.home.practice.spring.ant_matcher;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;

/**
 * classpath:
 * classpath*:
 *
 * PathMatchingResourcePatternResolver 实际上使用的也是 AntPathMatcher
 *  {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver.pathMatcher}
 *
 * @author guowm
 * @date 2023/1/12
 */
@Component
public class ClasspathAntMatcherDemo {

    @PostConstruct
    public void classpathAntPathMatcher() {
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resource1 = resolver.getResources("classpath:META-INF/**/*.txt");
            Arrays.stream(resource1).forEach(item -> {
                try {
                    item.getURI();
//                    System.out.println(item.getURI());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });


            System.out.println("======================================");

            Resource[] resource2 = resolver.getResources("classpath*:META-INF/**/*.txt");
            Arrays.stream(resource2).forEach(item -> {
                try {
                    item.getURI();
//                    System.out.println(item.getURI());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
